package com.yanxinim.cn.util.picker;

import android.app.Activity;
import android.graphics.Color;

import com.ypx.imagepicker.ImagePicker;
import com.ypx.imagepicker.bean.ImageItem;
import com.ypx.imagepicker.bean.MimeType;
import com.ypx.imagepicker.bean.PickerError;
import com.ypx.imagepicker.bean.SelectMode;
import com.ypx.imagepicker.bean.selectconfig.CropConfig;
import com.ypx.imagepicker.data.OnImagePickCompleteListener;
import com.ypx.imagepicker.data.OnImagePickCompleteListener2;

import java.util.ArrayList;


public class ImagePickerHelper {

    public static void pickSingleImage(Activity activity, OnImagePickCompleteListener2 listener2) {
        ImagePicker.withMulti(new ImagePickerPresenter())
                .setMaxCount(1)
                .setColumnCount(4)
                .setOriginal(false)
                .mimeTypes(MimeType.ofImage())
                .setDefaultOriginal(false)
                .showCamera(true)//显示拍照
                .showCameraOnlyInAllMediaSet(false)
                .setPreview(true)//是否开启预览
                .setSinglePickWithAutoComplete(false)
                .setSelectMode(SelectMode.MODE_SINGLE)
                .pick(activity, listener2);
    }

    public static void pickImage(Activity activity, int maxCount, OnImagePickCompleteListener2 listener2) {
        ImagePicker.withMulti(new ImagePickerPresenter())
                .setMaxCount(maxCount)
                .setColumnCount(4)
                .setOriginal(false)
                .mimeTypes(MimeType.ofImage())
                .setDefaultOriginal(false)
                .showCamera(true)//显示拍照
                .showCameraOnlyInAllMediaSet(false)
                .setPreview(true)//是否开启预览
                .setSinglePickWithAutoComplete(false)
                .setSelectMode(SelectMode.MODE_SINGLE)
                .pick(activity, listener2);
    }

    public static void pickSingleImageCrop(Activity activity, OnImagePickCompleteListener2 listener2) {
        ImagePicker.withMulti(new ImagePickerPresenter())
                .setMaxCount(1)
                .setColumnCount(4)
                .setOriginal(false)
                .mimeTypes(MimeType.ofImage())
                .setDefaultOriginal(false)
                .showCamera(true)//显示拍照
                .showCameraOnlyInAllMediaSet(false)
                .setPreview(true)//是否开启预览
                .setSinglePickWithAutoComplete(false)
                .setSelectMode(SelectMode.MODE_SINGLE)
                .pick(activity, new OnImagePickCompleteListener2() {
                    @Override
                    public void onPickFailed(PickerError error) {
                        listener2.onPickFailed(error);
                    }

                    @Override
                    public void onImagePickComplete(ArrayList<ImageItem> items) {
                        CropConfig cropConfig = new CropConfig();
                        //设置剪裁比例
                        cropConfig.setCropRatio(2, 1);
                        //设置剪裁框间距，单位px
                        cropConfig.setCropRectMargin(100);
                        //是否保存到DCIM目录下，false时会生成在 data/files/imagepicker/ 目录下
                        cropConfig.saveInDCIM(false);
                        //是否圆形剪裁，圆形剪裁时，setCropRatio无效
                        cropConfig.setCircle(false);
                        //设置剪裁模式，留白或充满  CropConfig.STYLE_GAP 或 CropConfig.STYLE_FILL
                        cropConfig.setCropStyle(CropConfig.STYLE_GAP);
                        //设置留白模式下生成的图片背景色，支持透明背景
                        cropConfig.setCropGapBackgroundColor(Color.TRANSPARENT);
                        //调用剪裁
                        ImagePicker.crop(activity, new ImagePickerPresenter(), cropConfig, items.get(0).getPath(),
                                new OnImagePickCompleteListener() {
                                    @Override
                                    public void onImagePickComplete(ArrayList<ImageItem> items) {
                                        //剪裁回调，主线程
                                        listener2.onImagePickComplete(items);
                                    }
                                });
                    }
                });
    }

    public static void pickSingleImageCropAvatar(Activity activity, OnImagePickCompleteListener2 listener2) {
        ImagePicker.withMulti(new ImagePickerPresenter())
                .setMaxCount(1)
                .setColumnCount(4)
                .setOriginal(false)
                .mimeTypes(MimeType.ofImage())
                .setDefaultOriginal(false)
                .showCamera(true)//显示拍照
                .showCameraOnlyInAllMediaSet(false)
                .setPreview(true)//是否开启预览
                .setSinglePickWithAutoComplete(false)
                .setSelectMode(SelectMode.MODE_SINGLE)
                .pick(activity, new OnImagePickCompleteListener2() {
                    @Override
                    public void onPickFailed(PickerError error) {
                        listener2.onPickFailed(error);
                    }

                    @Override
                    public void onImagePickComplete(ArrayList<ImageItem> items) {
                        CropConfig cropConfig = new CropConfig();
                        //设置剪裁比例
                        cropConfig.setCropRatio(1, 1);
                        //设置剪裁框间距，单位px
                        cropConfig.setCropRectMargin(100);
                        //是否保存到DCIM目录下，false时会生成在 data/files/imagepicker/ 目录下
                        cropConfig.saveInDCIM(false);
                        //是否圆形剪裁，圆形剪裁时，setCropRatio无效
                        cropConfig.setCircle(false);
                        //设置剪裁模式，留白或充满  CropConfig.STYLE_GAP 或 CropConfig.STYLE_FILL
                        cropConfig.setCropStyle(CropConfig.STYLE_GAP);
                        //设置留白模式下生成的图片背景色，支持透明背景
                        cropConfig.setCropGapBackgroundColor(Color.TRANSPARENT);
                        //调用剪裁
                        ImagePicker.crop(activity, new ImagePickerPresenter(), cropConfig, items.get(0).getPath(),
                                new OnImagePickCompleteListener() {
                                    @Override
                                    public void onImagePickComplete(ArrayList<ImageItem> items) {
                                        //剪裁回调，主线程
                                        listener2.onImagePickComplete(items);
                                    }
                                });
                    }
                });
    }
}
